package com.itheima.ssm.controller;

import com.itheima.ssm.domain.UserInfo;
import com.itheima.ssm.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

@Autowired
private IUserService userService;
    @RequestMapping("/findAll")
    public ModelAndView findAll(){
        ModelAndView mv = new ModelAndView();
        List<UserInfo> userInfoList = userService.findAll();
        System.out.println(userInfoList);
        mv.addObject("userlist",userInfoList);
        mv.setViewName("user-list");
        return mv;
    }
/*=================================================================================================*/
   /* @RequestMapping("/save")
    public String save (UserInfo users){
        userService.save(users);
        return "redirect:findAll";
    }*/
@RequestMapping("/save")
public String save (UserInfo users ,String[]ids){
    userService.save(users ,ids);
    return "redirect:findAll";
}

   /*===================================================================================*/
 @RequestMapping("/findById")
    public ModelAndView findById(String id){
      UserInfo userInfo  =userService.findById(id);
     ModelAndView mv = new ModelAndView();
     mv.addObject("user",userInfo);
     mv.setViewName("user-show");
     return mv;
 }

    @RequestMapping("/addRoleAndUser")
    public String addRoleAndUser(String uId,String[]rIds){
                userService.addRoleAndUser(uId,rIds);
        return "redirect:findAll";
    }

}
